package com.googlecode.skald.client.cmds;

 import com.google.gwt.user.client.ui.PopupPanel;

/**
 * Constructs an AffordanceCommand that, when executed, closes (hides) 
 * the associated pop-up and then executes the wrapped command.
 * 
 * @author Zach Tomaszewski
 */
public class PopupClosingCommand implements AffordanceCommand {

  private AffordanceCommand cmd;
  private PopupPanel popup;
  
  /**
   * Wraps the given cmd and, when executed, will close the given popup.
   */
  public PopupClosingCommand(AffordanceCommand cmd, PopupPanel popup) {
    this.cmd = cmd;
    this.popup = popup;
  }
  
  /** 
   * Returns a new copy of this Command that will close the same popup.
   * Passes the given cmdText onto the wrapped command in order to build 
   * a new wrapped command.
   */
  @Override
  public AffordanceCommand build(String cmdText) {
    return new PopupClosingCommand(cmd.build(cmdText), this.popup); 
  }

  /** Hides the associated popup and then executes the wrapped command. */
  @Override
  public void execute() {
    this.popup.hide();
    this.cmd.execute();
  }
}
